<?php
/**
 * ActivityController.class.php
 * 活动控制器
 * 
 * @author 	王中艺	<wangzy_smile@qq.com>
 * @date 	2017-05-12
 */

namespace Admin\Controller;

class ActivityController extends BaseController {

	/**
	 * 获取活动列表
	 * @param 	$p
	 * @param   $level [<description>]
	 * @return 	$list json
	 */
	function getList(){
		$level  = $this->request('level', false);
		$page 	= $this->request('p', 1);
		$pageHtml 	= true;

		$list 	= D('Activity')->getList($level, $page, $pageHtml);
		if($list === false){
			$this->outOInfo(D('Activity')->errMsg , 0);
		}
		$this->outOInfo(['list' => $list, 'page_html' => $pageHtml]);
	}

	/**
	 * 获取活动详情
	 * @param 	$activity_id
	 * @return 	$info
	 */
	function getItem(){
		$this->unEmptyParam(['activity_id']);

		$activityId 	= $this->iInfo['activity_id'];
		$activityInfo 	= D('Activity')->getItem($activityId);

		if($activityInfo === false)
			$this->outOInfo(D('Activity')->errMsg, 0);
		$this->outOInfo(['info' => $activityInfo]);
	}

	/**
	 * 添加活动
	 * @param 	$title 	标题
	 * @param 	$banner 横幅
	 * @param 	$audio 	声音
	 * @param　	$introduct 	简介
	 * @param 	$i_header
	 * @param 	$i_audio
	 * @param 	$i_footer
	 * @param 	$ii_cont
	 * @param 	$iii_cont
	 * @param   $level 
	 * @return  boolean
	 */
	function addItem(){
		$this->unEmptyParam(['title', 'banner', 'audio', 'introduct', 'content', 
			'i_header', 'i_audio', 'i_footer', 'ii_cont', 'iii_cont', 'level']);

		$title 	= $this->iInfo['title'];
		$banner 	= $this->iInfo['banner'];
		$audio 	= $this->iInfo['audio'];
		$introduct 	= $this->iInfo['introduct'];
		$content 	= $this->iInfo['content'];
		$iHeader 	= $this->iInfo['i_header'];
		$iAudio 	= $this->iInfo['i_audio'];
		$iFooter 	= $this->iInfo['i_footer'];
		$iiCont 	= $this->iInfo['ii_cont'];
		$iiiCont 	= $this->iInfo['iii_cont'];
		$level 	= $this->iInfo['level'];

		//参数处理
		$banner 	= strchr($banner, '/server');
		$audio 	= strchr($audio, '/server');
		$audioTime 	= get_audio_info(strchr($audio, 'Uploads'), 'playtime_seconds');
		$iAudio 	= strchr($iAudio, '/server');
		$iAudioTime 	= get_audio_info(strchr($iAudio, 'Uploads'), 'playtime_seconds');

		$result 	= D('Activity')->addItem($title, $banner, $audio, $audioTime, $introduct, 
			$content, $iHeader, $iAudio, $iAudioTime, $iFooter, $iiCont, $iiiCont, $level);

		if($result === false)
			$this->outOInfo(D('Activity')->errMsg, 0);

		$this->outOInfo('Oh yes!');
	}

	/**
	 * 更改信息
	 * @param 	$activity_id
	 * @param 	$activity_info
	 * @return 	boolean
	 */
	function updateItem(){
		$this->unEmptyParam(['activity_id', 'activity_info']);

		$activityId 	= $this->iInfo['activity_id'];
		$activityInfo 	= $this->iInfo['activity_info'];

		//字段处理
		if(isset($activityInfo['audio'])){
			$activityInfo['audio'] 	= strchr($activityInfo['audio'], '/server');
			$activityInfo['audio_time'] 	= get_audio_info(strchr($activityInfo['audio'], 'Uploads'), 'playtime_seconds');
		}
		if(isset($activityInfo['i_audio'])){
			$activityInfo['i_audio'] 	= strchr($activityInfo['i_audio'], '/server');
			$activityInfo['i_audio_time'] 	= get_audio_info(strchr($activityInfo['i_audio'], 'Uploads'), 'playtime_seconds');
		}
		if(isset($activityInfo['banner'])){
			$activityInfo['banner'] 	= strchr($activityInfo['banner'], '/server');
		}

		$result 	= D('Activity')->updateItem($activityId, $activityInfo);
		if($result === false)
			$this->outOInfo(D('Activity')->errMsg, 0);

		$this->outOInfo('Oh yes!');
	}

	/**
	 * 获取活动须知列表
	 * @param 	$activity_id
	 * @return 	$list
	 */
	function getKnownList(){
		$this->unEmptyParam(['activity_id']);

		$activityId 	= $this->request('activity_id');
		$knownList 	= D('Activity')->getKnownList($activityId);
		if($knownList === false)
			$this->outOInfo(D('Activity')->errMsg, 0);

		$this->outOInfo(['list' => $knownList]);
	}

	/**
	 * 添加活动须知
	 * @param 	$activity_id
	 * @param 	$title
	 * @param 	$audio
	 * @return 	boolean
	 */
	function addKnown(){
		$this->unEmptyParam(['activity_id', 'title', 'audio']);

		$activityId 	= $this->request('activity_id');
		$title 	= $this->request('title');
		$audio 	= $this->request('audio');

		//音频参数解析
		$audio 	= strchr($audio, '/server');
		$audioTime 	= get_audio_info(strchr($audio, 'Uploads'), 'playtime_seconds');

		$result 	= D('Activity')->addKnown($activityId, $title, $audio, $audioTime);
		if($result === false)
			$this->outOInfo(D('Activity')->errMsg, 0);

		$this->outOInfo();
	}

	/**
	 * 变更活动须知
	 * @param 	$known_id
	 * @param 	$known_info
	 * @return 	boolean
	 */
	function updateKnown(){
		$this->unEmptyParam(['known_id', 'known_info']);

		$knownId 	= $this->request('known_id');
		$knownInfo 	= $this->request('known_info');
		if($knownInfo['audio']){
			$knownInfo['audio']	= strchr($knownInfo['audio'], '/server');
			$knownInfo['audio_time'] 	= get_audio_info(strchr($knownInfo['audio'], 'Uploads'), 'playtime_seconds');
		}
		$result 	= D('Activity')->updateKnown($knownId, $knownInfo);
		if($result === false)
			$this->outOInfo(D('Activity')->errMsg, 0);

		$this->outOInfo();
	}

	/**
	 * 获取相册列表
	 * @param 	$keyword
	 * @param 	$date_end
	 * @param 	$date_end
	 * @param 	$p
	 * @return 	$list
	 */
	function getPictureList(){
		$keyword 	= $this->request('keyword', false);
		$timeS 	= $this->request('date_start') ? strtotime($this->request('date_start')) : false;
		$timeE 	= $this->request('date_end') ? strtotime($this->request('date_end')) + 3600*24 : false;
		$page 	= $this->request('p', 1);
		$pageHtml 	= true;

		$list 	= D('Activity')->getPictureList($keyword, $timeS, $timeE, $page, $pageHtml);
		if($list === false)
			$this->outOInfo(D('Activity')->errMsg, 0);
		$this->outOInfo(['list' => $list, 'page_html' => $pageHtml]);
	}
}